<template>
<div id="home">
  <div style="margin: 10px 0">
    <el-button type="primary" @click="add">新增</el-button>
  </div>
  <div style="margin: 10px 0">
    <el-input v-model="search" placeholder="请输入关键字" style="width: 20%"></el-input>
    <el-button type="primary" @click="load">查询</el-button>
  </div>
  <el-table
    :data="tableData"
    border
    style="width: 100%">
    <el-table-column
      fixed
      prop="id"
      label="id"
      width="150">
    </el-table-column>
    <el-table-column
      prop="name"
      label="姓名"
      width="120">
    </el-table-column>
    <el-table-column
      prop="passWord"
      label="密码"
      width="300">
    </el-table-column>
    <el-table-column
      prop="sex"
      label="性别"
      width="300">
    </el-table-column>
    <el-table-column fixed="right" label="操作" width="100">
      <template slot-scope="scope">
        <el-button @click="handleClick(scope.row)" type="text">编辑</el-button>
       <el-popconfirm title="确定删除吗" @confirm="handleDelete(scope.row.id)">
          <template #reference>
          <el-button slot="reference" type="danger" >删除</el-button>
          </template>
       </el-popconfirm>
      </template>

    </el-table-column>
  </el-table>
  <div style="margin: 10px 0">
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage4"
      :page-sizes="[5, 10, 20]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
      <el-form :model="form"  label-width="120px">
        <el-form-item label="用户名">
          <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="form.userPassword"></el-input>
        </el-form-item>
        <el-form-item label="性别">
          <el-radio v-model="form.sex" label="1">男</el-radio>
          <el-radio v-model="form.sex" label="2">女</el-radio>
<!--       <el-input v-model="form.username"></el-input>-->
     </el-form-item>
   </el-form>
    <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="save">确 定</el-button>
  </span>
    </el-dialog>

  </div>
</div>
</template>

<script>
  import request from '@/utils/request'

  export default {
    name: 'Home',
    components:{

    },

    data() {
      return {
        form: {},
        dialogVisible: false,
        search: '',
        currentPage4: 1,
        pageSize: 5,
        total:0,
      }
    },
    created () {
      this.load()
    },
    methods: {
      load(){
        request.get("/api/user",{
          params: {
            pageNum: this.currentPage4,
            pageSize:this.pageSize,
            search: this.search
          }

        }).then(res =>{
          console.log(res)
          this.tableData=res.data.records
          this.total=res.data.total
        })
      },
      add(){
        this.dialogVisible=true
        this.form ={}
      },
      save(){
        if (this.form.id){  //更新
          request.put("/api/user",this.form).then(res =>{
            console.log(res)
            if(res.code=="0"){
              this.$message({
                type: "success",
                message: "更新成功"
              })
            }else {
              this.$message({
                type: "error",
                message: res.msg
              })
            }
            this.load()  //刷新表格数据
            this.dialogVisible=false  //关闭弹窗
          })
        }else {  //新增
          request.post("/api/user",this.form).then(res =>{
            console.log(res)
          })
          if(res.code=="0"){
            this.$message({
              type: "success",
              message: "更新成功"
            })
          }else {
            this.$message({
              type: "error",
              message: res.msg
            })
          }
          this.load()  //刷新表格数据
          this.dialogVisible=false  //关闭弹窗
        }

      },
      handleClick(row){
        this.form = JSON.parse(JSON.stringify(row))
        this.dialogVisible =true
      },
      handleDelete(id){
        request.delete("/api/user/"+id).then(res =>{
          if(res.code=="0"){
            this.$message({
              type: "success",
              message: "删除成功"
            })
          }else {
            this.$message({
              type: "error",
              message: res.msg
            })
          }
          this.load()  //重新加载
        })
      },
      handleSizeChange(pageSize){  //改变当前每页的个数触发
        this.pageSize =pageSize
        this.load()
      },
      handleCurrentChange(pageNum){  //改变当前页码触发
        this.currentPage4=pageNum
        this.load()
      }
    }
  }
</script>

<style scoped>

</style>
